package com.utils.web.spring.config;
//package com.utils.spring.config;
//
//import java.util.List;
//import java.util.Set;
//
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.ComponentScan;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Import;
//import org.springframework.data.redis.connection.RedisSentinelConfiguration;
//import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
//import org.springframework.data.redis.serializer.StringRedisSerializer;
//
//import com.fasterxml.jackson.annotation.JsonAutoDetect;
//import com.fasterxml.jackson.annotation.PropertyAccessor;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.google.common.collect.Lists;
//import com.google.common.collect.Sets;
//
//import lombok.extern.slf4j.Slf4j;
//
//
//@Slf4j
//@Configuration
//@Import(PropertyConfig.class)
//@ComponentScan("com.kyhd.bidder.redis")
//public class RedisHaConfiguration {
//	
//	@Value("${redis.sentinel.nodes}")
//    private String sentinelNodes;
//	
//	int timeout = 100;
//	
//	
//	@Bean
//	JedisConnectionFactory jedisConnectionFactory() {
//		RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration();
//		
//		log.info("sentinelnodes:{}",sentinelNodes);
//		List<String> nodeList = Lists.newArrayList(sentinelNodes.split(","));
//		if(nodeList.size()>=2){
//			String master = nodeList.remove(0);
//			Set<String> sentinelNodes = Sets.newHashSet(nodeList);
//			log.info("master:{},sentinelNodes:{}",master,sentinelNodes);
//			sentinelConfiguration = new RedisSentinelConfiguration(master,sentinelNodes);
//			
//		}else{
//			throw new RuntimeException("redis.sentinel.nodes: the num of nodes great than eqauls two at least.");
//		}
//
//	    JedisConnectionFactory jedisConFactory = new JedisConnectionFactory(sentinelConfiguration);
//	    jedisConFactory.setTimeout(timeout);
//	    log.info("host:{},port:{}",jedisConFactory.getHostName(),jedisConFactory.getPort());
//	    jedisConFactory.setDatabase(11);
///*		GenericObjectPoolConfig config = new GenericObjectPoolConfig();
//        config.setMaxTotal(maxTotal);
//        config.setMaxIdle(maxIdle);
//        config.setMinIdle(minIdle);//设置最小空闲数
//        config.setMaxWaitMillis(maxWait);
//		//在获取Jedis连接时，自动检验连接是否可用
//		config.setTestOnBorrow(true);
//		//在将连接放回池中前，自动检验连接是否有效
//		config.setTestOnReturn(true);
//		//自动测试池中的空闲连接是否都是可用连接
//		config.setTestWhileIdle(true);
//		//连接耗尽时是否阻塞, false报异常,ture阻塞直到超时,默认true
//		config.setBlockWhenExhausted(false);
//        //表示idle object evitor两次扫描之间要sleep的毫秒数
//        config.setTimeBetweenEvictionRunsMillis(30000);
//        //表示idle object evitor每次扫描的最多的对象数
//        config.setNumTestsPerEvictionRun(10);
//        //表示一个对象至少停留在idle状态的最短时间，然后才能被idle object evitor扫描并驱逐；这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义
//        config.setMinEvictableIdleTimeMillis(60000);
//      //需要密码连接的创建对象方式
//        //参数依次是：集群地址，链接超时时间，返回值的超时时间，链接尝试次数，密码和配置文件
//        return new JedisCluster(nodes,commandTimeout,10000,3,pwd,config);*/
//	    return jedisConFactory;
//	}
//	
//
//	
//	
//	@Bean(name="redisTemplate")
//	public RedisTemplate<String, Object> redisTemplate() {
////	    RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
////	    template.setConnectionFactory(jedisConnectionFactory());
////	    template.setKeySerializer(new StringRedisSerializer());
////	    template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
////	    template.afterPropertiesSet();
//	    //设置序列化
//        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//        ObjectMapper om = new ObjectMapper();
//        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        jackson2JsonRedisSerializer.setObjectMapper(om);
//        // 配置redisTemplate
//        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
//        redisTemplate.setConnectionFactory(jedisConnectionFactory());
//        redisTemplate.setKeySerializer(new StringRedisSerializer()); // key序列化
////        redisTemplate.setValueSerializer(new GenericToStringSerializer<Object>(Object.class)); // value序列化
//        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); // value序列化
//        redisTemplate.setHashKeySerializer(new StringRedisSerializer()); // Hash key序列化
//        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); // Hash value序列化
//        redisTemplate.afterPropertiesSet();
//	    return redisTemplate;
//	}
//	
//	
//
//}
